package com.fyyg.common.util.popular.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;


/**
 * XML 数据接收对象转换工具类
 * @author LiYi
 *
 */
@SuppressWarnings("restriction")
public class XMLConverUtil{
	
	
//	
//	private static Map<Class<?>,Unmarshaller> uMap = new HashMap<Class<?>,Unmarshaller>();
//	private static Map<Class<?>,Marshaller> mMap = new HashMap<Class<?>,Marshaller>();
//	
	/**
	 * XML to Object
	 * @param <T>
	 * @param xml
	 * @return
	 */
	public static <T> T convertToObject(Class<T> clazz,String xml){
		return convertToObject(clazz,new StringReader(xml));
	}
	
	/**
	 * XML to Object
	 * @param <T>
	 * @param clazz
	 * @param inputStream
	 * @return
	 */
	public static <T> T convertToObject(Class<T> clazz,InputStream inputStream){
		return convertToObject(clazz,new InputStreamReader(inputStream));
	}
	
	/**
	 * XML to Object
	 * @param <T>
	 * @param clazz
	 * @param reader
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T convertToObject(Class<T> clazz,Reader reader){
//		try {
//			if(!uMap.containsKey(clazz)){
//				JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
//				Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
//				uMap.put(clazz,unmarshaller);
//			}
//			return (T)uMap.get(clazz).unmarshal(reader);
//		} catch (JAXBException e) {
//			e.printStackTrace();
//		}
		return null;
	}
	
	/**
	 * Object to XML
	 * @param object
	 * @return
	 */
	public static String convertToXML(Object object){
//		try {
//			if(!mMap.containsKey(object.getClass())){
//				JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
//				Marshaller marshaller = jaxbContext.createMarshaller();
//				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//	            marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
//	                public void escape(char[] ac, int i, int j, boolean flag,Writer writer) throws IOException {
//	                writer.write( ac, i, j ); }
//	            });
//				mMap.put(object.getClass(), marshaller);
//			}
//			StringWriter stringWriter = new StringWriter();
//			mMap.get(object.getClass()).marshal(object,stringWriter);
//			return stringWriter.getBuffer().toString();
//		} catch (JAXBException e) {
//			e.printStackTrace();
//		}
		return null;
	}
}
